using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XoniXNA
{
    public class Constants
    {
        public const int CELL_SIZE = 40;
    }
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class XonixGame : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        public SpriteBatch SpriteBatch
        {
            get { return spriteBatch; }
            set { spriteBatch = value; }
        }
        SpriteFont gameFont;

        Texture2D backTexture;
        Texture2D borderTexture;
        Player firstPlayer;
        Vector2 startLocation;
        Vector2 padding;

        public Vector2 BoardPadding
        {
            get { return padding; }
            set { padding = value; }
        }
        int nWidth;

        public int BoardWidth
        {
            get { return nWidth; }
            set { nWidth = value; }
        } 
        int nHeight;

        public int BoardHeight
        {
            get { return nHeight; }
            set { nHeight = value; }
        }

        

        public XonixGame()
        {
            graphics = new GraphicsDeviceManager(this);

            // put the game in fullscreen mode;
            graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
            graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
            graphics.IsFullScreen = true;

            padding = new Vector2();
            padding.X = graphics.PreferredBackBufferWidth % Constants.CELL_SIZE / 2;
            padding.Y = graphics.PreferredBackBufferHeight % Constants.CELL_SIZE / 2;
            nWidth = graphics.PreferredBackBufferWidth / Constants.CELL_SIZE;
            nHeight = graphics.PreferredBackBufferHeight / Constants.CELL_SIZE;
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            startLocation = new Vector2();
            startLocation.X = padding.X + nWidth / 2 * Constants.CELL_SIZE;
            startLocation.Y = padding.Y + Constants.CELL_SIZE;
            PlayerDescription desc = new PlayerDescription(string.Empty, 0, startLocation);
            firstPlayer = new Player(this, desc);
            Components.Add(firstPlayer);
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            backTexture = Content.Load<Texture2D>("texture002");
            borderTexture = Content.Load<Texture2D>("barTexture");
            gameFont = Content.Load<SpriteFont>("GameFont");
            firstPlayer.PlayerTexture = Content.Load<Texture2D>("player");
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            // TODO: Add your update logic here
            bool bPressed = false;
            KeyboardState keyState = Keyboard.GetState();
            if (keyState.IsKeyDown(Keys.Right))
            {
                firstPlayer.Move(Direction.Right);
                bPressed = true;
            }

            if (!bPressed && keyState.IsKeyDown(Keys.Left))
            {
                firstPlayer.Move(Direction.Left);
                bPressed = true;
            }

            if (!bPressed && keyState.IsKeyDown(Keys.Up))
            {
                firstPlayer.Move(Direction.Up);
                bPressed = true;
            }

            if (!bPressed && keyState.IsKeyDown(Keys.Down))
            {
                firstPlayer.Move(Direction.Down);
                bPressed = true;
            }

            ;
            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.White);
            int width = nWidth * Constants.CELL_SIZE;
            int height = (nHeight - 1) * Constants.CELL_SIZE;
            // TODO: Add your drawing code here
            spriteBatch.Begin();
            spriteBatch.Draw(backTexture, new Rectangle((int) padding.X, (int) padding.Y + Constants.CELL_SIZE, width, height), Color.White);
            spriteBatch.Draw(borderTexture,
                new Rectangle((int)padding.X, (int)padding.Y + Constants.CELL_SIZE, width, Constants.CELL_SIZE), Color.White);
            spriteBatch.Draw(borderTexture, new Rectangle((int) padding.X, (int)padding.Y + Constants.CELL_SIZE, 
                Constants.CELL_SIZE, height), Color.White);
            spriteBatch.Draw(borderTexture, new Rectangle((int)padding.X, (int)padding.Y + height, width, Constants.CELL_SIZE),
                Color.White);
            spriteBatch.Draw(borderTexture, 
                new Rectangle((int)padding.X + width - Constants.CELL_SIZE,(int)padding.Y + Constants.CELL_SIZE,
                    Constants.CELL_SIZE, height), Color.White);
            spriteBatch.DrawString(gameFont, "Score: 000000", padding, Color.Red);          
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
